Linux File System
Important Linux Directories
Symbolic Link Directories
File Management
Create Directories
Copy, Move, and Remove Files and Directories
Create Hard Links
Create Soft Links
File systems organized into a single inverted tree of directories
Root of tree is at top of hierarchy
Branches of directories and subdirectories are below
/ is root directory at top of file system hierarchy
/ character also used as directory separator in file names
Example: etc subdirectory of / is /etc
Example: issue file within /etc is /etc/issue
Subdirectories of / are used for standard purposes to organize
files
Example: In / root directory, /boot stores files needed to boot system
Static: Content that remains unchanged until explicitly edited or reconfigured
Dynamic or variable: Content that is modified or appended by active processes
Persistent: Content, particularly configuration settings, that remains after a reboot
Runtime: Process- and system-specific content and attributes that are cleared during a reboot
Location | Purpose |
| Installed software, shared libraries, include files, and static read-only program data. Important subdirectories include:
|
| Configuration files specific to system. |
| Variable data specific to system that should persist between boots. Under Examples: Databases, cache directories, log files, printer-spooled documents, and website content. |
| Runtime data for processes started since last boot. Includes process ID files and lock files. Contents of
|
| Where regular users store personal data and configuration files. |
| Home directory for |
| World-writable space for temporary files. Files that are not accessed, changed, or modified for 10 days are deleted automatically.
|
| Contains files needed to start boot process. |
| Contains special device files that are used by system to access hardware. |
Older directories in / now have the same content as their counterparts in /usr:
/bin and /usr/bin
/sbin and /usr/sbin
/lib and /usr/lib
/lib64 and /usr/lib64
Formerly these were distinct directories with different files
Directories in / are symbolic links to matching directories in /usr
hier(7) man page
This table describes common file management commands and how they are used in single-source and multiple-source situations.
Task | Single Source (note) | Multiple Source (note) |
Copy file |
|
|
Move file |
|
|
Remove file |
|
|
Create directory |
|
|
Copy directory |
|
|
Move directory |
|
|
Remove directory |
|
|
(1)The result is a rename. (2)The recursive option is required to process a source directory. (3)If dir2 exists, the result is a move. If dir2 does not exist, the result is a rename. (4)The last argument must be a directory. (5)Use caution with the force option. You are not prompted to confirm your action. (6)Use caution with the create-parent option. Typing errors are not reported or corrected. |
Use mkdir to create directories and subdirectories
Error conditions:
Name already exists
Parent directory does not exist
Use -p to create missing parent directories for requested destination
Spelling and typing mistakes create unintended directories without generating error messages
Example: mkdir error condition
[student@desktop1 ~]$ mkdir Video/Watched mkdir:cannot create directory `Video/Watched': No such file or directory
[student@desktop1 ~]$ mkdir Videos/Watched [student@desktop1 ~]$ cd Documents [student@desktop1 Documents]$ mkdir ProjectX ProjectY [student@desktop1 Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3 [student@desktop1 Documents]$ cd [student@desktop1 ~]$ ls -R Videos Documents Documents: ProjectX ProjectY Thesis thesis_chapter1.odf thesis_chapter2.odf Documents/ProjectX: Documents/ProjectY: Documents/Thesis: Chapter1 Chapter2 Chapter3 Documents/Thesis/Chapter1: Documents/Thesis/Chapter2: Documents/Thesis/Chapter3: Videos: blockbuster1.ogg blockbuster2.ogg Watched Videos/Watched: [student@desktop1 ~]$
Use cp to copy one or more files to new, independent files
Copy file to new file in current directory or another
Copy multiple files to another directory
New file names must be unique
If new file name is not unique, cp overwrites existing file
[student@desktop1 ~]$ cd Videos [student@desktop1 Videos]$ cp blockbuster1.ogg blockbuster3.ogg [student@desktop1 Videos]$ ls -l total 0 -rw-rw-r--. 1 student student 0 Feb 8 16:3 blockbuster1.ogg -rw-rw-r--. 1 student student 0 Feb 8 16:4 blockbuster2.ogg -rw-rw-r--. 1 student student 0 Feb 8 19:2 blockbuster3.ogg drwxrwxr-x. 2 student student 4096 Feb 8 23:5 Watched [student@desktop1 Videos]$
When copying multiple files with one command, last argument must be a directory
Copied files retain original names in new directory
Conflicting file names at destination result in overwritten files
To prevent overwriting, multi-file cp ignores directories specified as source
Use -r to copy non-empty directories
[student@desktop1 Videos]$ cd ../Documents [student@desktop1 Documents]$ cp thesis_chapter1.odf thesis_chapter2.odf Thesis ProjectX cp:omitting directory `Thesis' [student@desktop1 Documents]$ cp -r Thesis ProjectX [student@desktop1 Documents]$ cp thesis_chapter2.odf Thesis/Chapter2/ [student@desktop1 Documents]$ ls -R .: ProjectX ProjectY Thesis thesis_chapter1.odf thesis_chapter2.odf ./ProjectX: Thesis thesis_chapter1.odf thesis_chapter2.odf ./ProjectX/Thesis: ./ProjectY: ./Thesis: Chapter1 Chapter2 Chapter3 ./Thesis/Chapter1: ./Thesis/Chapter2: thesis_chapter2.odf ./Thesis/Chapter3: [student@desktop1 Documents]$
Use mv to rename files in same directory or relocate files to new directory
File content remains unchanged
To move files to different file system, create new file by copying and then delete source file
Large files may take longer to move
[student@desktop1 Videos]$ cd ../Documents [student@desktop1 Documents]$ ls -l total 0 -rw-rw-r--. 1 student student 0 Feb 8 16:4 thesis_chapter1.odf -rw-rw-r--. 1 student student 0 Feb 8 16:4 thesis_chapter2.odf [student@desktop1 Documents]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf [student@desktop1 Documents]$ mv thesis_chapter1.odf Thesis/Chapter1 [student@desktop1 Documents]$ ls -lR .: total 16 drwxrwxr-x. 2 student student 4096 Feb 11 11:8 ProjectX drwxrwxr-x. 2 student student 4096 Feb 11 11:5 ProjectY drwxrwxr-x. 5 student student 4096 Feb 11 11:6 Thesis -rw-rw-r--. 1 student student 0 Feb 11 11:4 thesis_chapter2_reviewed.odf ./ProjectX: total 0 -rw-rw-r--. 1 student student 0 Feb 11 11:8 thesis_chapter1.odf -rw-rw-r--. 1 student student 0 Feb 11 11:8 thesis_chapter2.odf ./ProjectX/Thesis: total 0 ./ProjectY: total 0 ./Thesis: total 12 drwxrwxr-x. 2 student student 4096 Feb 11 11:9 Chapter1 drwxrwxr-x. 2 student student 4096 Feb 11 11:6 Chapter2 drwxrwxr-x. 2 student student 4096 Feb 11 11:6 Chapter3 ./Thesis/Chapter1: total 0 -rw-rw-r--. 1 student student 0 Feb 11 11:4 thesis_chapter1.odf ./Thesis/Chapter2: total 0 -rw-rw-r--. 1 student student 0 Feb 11 11:4 thesis_chapter2.odf ./Thesis/Chapter3: total 0 [student@desktop1 Documents]$
Default syntax for rm deletes files but not directories
To delete directories and subdirectories, use -r
To receive prompt for each deletion, use -i
To force deletion without prompts, use -f
| There is no undelete command or trash bin from which to restore. |
[student@desktop1 Documents]$ pwd /home/student/Documents [student@desktop1 Documents]$ rm thesis_chapter2_reviewed.odf [student@desktop1 Documents]$ rm Thesis/Chapter1 rm:cannot remove `Thesis/Chapter1': Is a directory [student@desktop1 Documents]$ rm -r Thesis/Chapter1 [student@desktop1 Documents]$ ls -l Thesis total 8 drwxrwxr-x. 2 student student 4096 Feb 11 12:7 Chapter2 drwxrwxr-x. 2 student student 4096 Feb 11 12:8 Chapter3 [student@desktop1 Documents]$ rm -ri Thesis rm:descend into directory `Thesis'? y rm:descend into directory `Thesis/Chapter2'? y rm:remove regular empty file `Thesis/Chapter2/thesis_chapter2.odf'? y rm:remove directory `Thesis/Chapter2'? y rm:remove directory `Thesis/Chapter3'? y rm:remove directory `Thesis'? y [student@desktop1 Documents]$
To delete only empty directories, use rmdir
Removed directories cannot be undeleted
[student@desktop1 Documents]$ pwd /home/student/Documents [student@desktop1 Documents]$ rmdir ProjectY [student@desktop1 Documents]$ rmdir ProjectX rmdir:failed to remove `ProjectX': Directory not empty [student@desktop1 Documents]$ rm -r ProjectX [student@desktop1 Documents]$ ls -lR .: total 0 [student@desktop1 Documents]$
Man pages: cp(1), mkdir(1), mv(1), rm(1), and rmdir(1)
Hard link: Directory entry that references a file
Every file has one hard link by default
To save space, create hard link instead of copying file
If created in same directory as existing hard link, hard link must have different file name
Or create hard link in different directory
All hard links pointing to file have same:
Permissions
Link count
User/group ownerships
Time stamps
Content
Hard links pointing to file must be on same file system
To show hard link count, use ls -l
Count appears after permissions and before file owner
[root@server1 ~]# echo "Hello World" > newfile.txt [root@server1 ~]# ls -l newfile.txt -rw-r--r--. 1 root root 0 Mar 11 19:9 newfile.txt
To create hard link to file, use ln
ln expects existing file as first argument, followed by one or more hard links
Hard links can reside anywhere on same file system as existing file
Note: If you create a hard link, you cannot easily tell that a file is a hard link or where it links to
Example: Create hard link newfile-link2.txt for file newfile.txt in same directory
[root@server1 ~]# ln newfile.txt /tmp/newfile-hlink2.txt [root@server1 ~]# ls -l newfile.txt /tmp/newfile-hlink2.txt -rw-rw-r--. 2 root root 12 Mar 11 19:9 newfile.txt -rw-rw-r--. 2 root root 12 Mar 11 19:9 newfile-hlink2.txt
Example: If original file is deleted, content of file is available as long as one hard link exists
[root@server1 ~]# rm -f newfile.txt [root@server1 ~]# ls -l /tmp/newfile-link2.txt -rw-rw-r--. 1 root root 12 Mar 11 19:9 /tmp/newfile-link2.txt [root@server1 ~]# cat /tmp/newfile-link2.txt Hello World
| All hard links referencing a file have the same permissions, link count, user/group ownerships, time stamps, and file contents. Any information changed on one hard link also changes on all other hard links. |
Soft link: Special file that points to existing file or directory
Can point to file or directory on another file system
To create a soft, or symbolic, link, use ln -s
[root@server1 ~]# ln -s /root/newfile-link2.txt /tmp/newfile-symlink.txt [root@server1 ~]# ls -l newfile-link2.txt /tmp/newfile-symlink.txt lrwxrwxrwx. 1 root root 11 Mar 11 20:9 /tmp/newfile-symlink.txt -> /root/newfile-link2.txt -rw-rw-r--. 1 root root 12 Mar 11 19:9 newfile-link2.txt
Dangling soft link: Soft link pointing to missing file
[root@server1 ~]# rm -f newfile-link2.txt [root@server1 ~]# ls -l /tmp/newfile-symlink.txt lrwxrwxrwx. 1 root root 11 Mar 11 20:9 /tmp/newfile-symlink.txt -> newfile-link2.txt [root@server1 ~]# cat /tmp/newfile-symlink.txt cat:/tmp/newfile-symlink.txt: No such file or directory
Soft link can point to and act like directory
To change to soft link directory, use cd
Example: Create soft link /root/configfiles pointing to /etc
[root@server1 ~]# ln -s /etc /root/configfiles [root@server1 ~]# cd /root/configfiles [root@server1 configfiles]# pwd /root/configfiles
ln(1) man page
Linux File System
Important Linux Directories
Symbolic Link Directories
File Management
Create Directories
Copy, Move, and Remove Files and Directories
Create Hard Links
Create Soft Links
Nice job!
Click the button below to complete this module of the course:
Click the button below to continue to the course homepage:
Please continue with the next item in the course.